<?php
/**
 * @name LayoutPlugin
 * @desc Yaf定义了如下的6个Hook,插件之间的执行顺序是先进先Call
 * @see http://www.php.net/manual/en/class.yaf-plugin-abstract.php
 * @author root
 */

//class SamplePlugin extends Yaf_Plugin_Abstract {
class LayoutPlugin extends Yaf\Plugin_Abstract
{

    public function routerStartup(Yaf\Request_Abstract $request, Yaf\Response_Abstract $response)
    {
    }

    public function routerShutdown(Yaf\Request_Abstract $request, Yaf\Response_Abstract $response)
    {
        $config = \Yaf\Registry::get('config');
        $view_engine = $config->application->view->engine;
        $dispatcher = Yaf\Dispatcher::getInstance();

//路由结束已得到模块，此时灵活设置模版路径
        $moduleName = $request->getModuleName();
        $viewScriptPath = APP_PATH . '/';
        if ($moduleName != 'Index') {
            $viewScriptPath .= 'modules/' . $moduleName . '/views';
        } else {
            $viewScriptPath .= 'views';
        }

        if ($view_engine == 'twig') {//twig模板引擎
            $twig = new \Twig\Adapter(null, $config->get("twig")->toArray());
            $twig->setScriptPath($viewScriptPath);
            $dispatcher->setView($twig);
        } elseif ($view_engine == 'smarty') {//smarty模板引擎
            $smarty = new \Smarty\Adapter(null, $config->smarty->toArray());
            $smarty->setScriptPath($viewScriptPath);
            $dispatcher->setView($smarty);
        }
        //整合smarty
//         $dispatcher->disableView();
//        $dispatcher->autoRender(false);
    }

    public function dispatchLoopStartup(Yaf\Request_Abstract $request, Yaf\Response_Abstract $response)
    {
    }

    public function preDispatch(Yaf\Request_Abstract $request, Yaf\Response_Abstract $response)
    {
    }

    public function postDispatch(Yaf\Request_Abstract $request, Yaf\Response_Abstract $response)
    {
    }

    public function dispatchLoopShutdown(Yaf\Request_Abstract $request, Yaf\Response_Abstract $response)
    {
    }
}
